package org.lyy.controller;


import lombok.experimental.var;
import org.lyy.entity.ProductInfo;
import org.lyy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin//跨域
@RestController//@Controller+@ResponseBody
@RequestMapping("/product/")

public class ProductInfoController {
    @Autowired
    private ProductService productService;
    //search,把库存表整个返回给前端
    //添加数据，一行一行来
    //修改数据，一行一行来
    //以上两者，如果返回来的id数据库没有，则插入新的一项，如果有，则修改数据库，看一下前端怎么按这一行

    @RequestMapping("show")
    public List<ProductInfo> search(){
        return productService.findUpAll();
    }
    @RequestMapping("searchId")
    public ProductInfo searchId(HttpServletRequest request){
        ProductInfo pI = new ProductInfo();
        Map<String, Object> retMap = new HashMap<>();
        String productID = request.getParameter("productId");
        String productName = request.getParameter("productName");
        String productStock = request.getParameter("productStock");
        Integer p_Stock = Integer.valueOf(productStock);
        String productPrice = request.getParameter("productPrice");
        String productKind = request.getParameter("productKind");
        Integer p_Kind = Integer.valueOf(productKind);
        String productStatus = request.getParameter("productStatus");
        Integer p_Status = Integer.valueOf(productStatus);
        List<ProductInfo> p = productService.findOne(productID);
        System.out.println("hahh"+p.size());
        for(ProductInfo product : p){
            //库存表中不存在这个数据
            if(product.getProductId().equals(productID)) {

                pI.setProductId(productID);
                pI.setProductName(productName);
                pI.setProductStock(p_Stock);
                pI.setProductPrice(productPrice);
                pI.setProductKind(p_Kind);
                pI.setProductStatus(p_Status);
                int result = productService.update(pI);
                System.out.println("test1"+pI.getProductId());
                System.out.println(result);
                retMap.put("msg", result+"success-val");
                return pI;
            }
        }
        if(p.size() == 0){
            pI.setProductId(productID);
            pI.setProductName(productName);
            pI.setProductStock(p_Stock);
            pI.setProductPrice(productPrice);
            pI.setProductKind(p_Kind);
            pI.setProductStatus(p_Status);
            productService.insertProduct(pI);
            System.out.println("testggg"+pI.getProductId());
            return pI;
        }
        return  pI;
    }
    @RequestMapping("delete")
    public Map<String, Object> deleteId(String productId){
        Map<String, Object> retMap = new HashMap<>();
        //String deleteId = request.getParameter("productId");
        //System.out.println("deletethahahha"+productId);
        productService.deleteProduct(productId);
        retMap.put("msg", "success-val");
       // System.out.println(deleteId);
        return  retMap;
    }

    @RequestMapping("onshow")
    public List<ProductInfo> onshowProduct(String productStatus){
        return productService.selectStatus(productStatus);
    }

    @RequestMapping("onKindF")
    public List<ProductInfo> showKindniupai(String productKind){
        return productService.selectKind(productKind);
    }
}
